<%@ page import="cn.gpnu.domain.User" %>
<%@ page import="cn.gpnu.dao.Userdao" %>
<%@ page import="cn.gpnu.domain.Tree" %>
<%@ page import="java.util.List" %>
<%@ page import="cn.gpnu.domain.TreePrice" %>
<%@ page import="cn.gpnu.domain.User" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<jsp:include page="导航条.jsp"/>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>树种更换</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
    <!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
    <script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>
    <base href="<%=basePath%>">
    <title>My JSP 'demo.jsp' starting page</title>
</head>
<style>

    table{
        width: 600px;
        height: 600px;
        margin: auto;
    }
    td{
        width: 180px;
        text-align: center;
    }
    tr{
        width: 600px;
    }
    .form_search{
        margin-right: 330px;
    }
    .rg_center{
        border: 4px solid skyblue;
        margin: auto;
        width: 600px;
        margin-top: 20px;
    }


    .change{
        width: 150px;
        height: 150px;
    }
    .gold_price{
        margin-left: 20px;
        color: black;
        font-size: 120%;
    }

    .gold_pic_price{
        width: 20px;
        height: 20px;
        margin-bottom: 5px;
    }

    .bottom_botton{

    }
    .btn_01{
        margin-left: 30px;
        margin-bottom: 10px;
    }
</style>
<body>
<%--<body onload="doit()">--%>
<%
    List<Tree> query = (List<Tree>) request.getAttribute("query");
//    for (Tree tree : query) {
//        System.out.println(tree.getTree_state());
//    }
%>

<%
    List<TreePrice> treePrices = (List<TreePrice>) request.getAttribute("treePrices");
    int []price = new int[10];
    int price_num = 1;
    for (TreePrice treePrice : treePrices) {
        price[price_num] = Integer.parseInt(treePrice.getTree_price());
        price_num++;
    }
%>
<%
    User user = (User) request.getAttribute("user");
%>


<input type="hidden" id="id" value="<%=user.getId()%>">

<div class="rg_center">
    <table class="table table-striped table table-bordered  table-hover">

        <%
            int []Array = new int[10];
            int i = 1;
        %>
        <% for(Tree tree : query){%>
        <tr>
            <td>
                <img src="img/树<%=tree.getTree_id()%><%=tree.getTree_state()%>.png" class="change" >
                <span class="gold_price">
                   (<%=tree.getTree_id()%>)&nbsp;&nbsp;<%Array[i]=tree.getTree_state();%><%=price[i]%>
                    <%System.out.println(tree.getTree_state()+" ");%>
                </span>
                <img src="img/gold.png" class="gold_pic_price" >
            </td>
        </tr>
        <%i++;}%>
    </table>
    <input type="number"  class="input_01" id = "tree_id" placeholder="请输入你想要的树种">
    <div class="bottom_botton">
        <button id = "btn_01" type="submit" class="btn btn_01 btn-success">Buy</button>
        <button id = "btn_02" type="submit" class="btn btn_01 btn-success">Sell</button>
        <button id = "btn_03" type="submit" class="btn btn_01 btn-success">Use</button>
    </div>
<%--    <button id = "btn_04" type="submit" class="btn btn-success">Search</button>--%>

</div>
<!--    <div class="progress progress-striped active">-->
<!--        <div id = "progress_bar"class="progress-bar progress-bar-success div_2" role="progressbar"-->
<!--             aria-valuenow="60" aria-valuemin="0" aria-valuemax="100"-->
<!--             style="width: 20%;">-->
<!--            &lt;!&ndash;        <span class="sr-only">40% 完成</span>&ndash;&gt;-->
<!--        </div>-->
<!--        <br>-->
<!--    </div>-->
<!--    <button id = "btn_01" type="submit" class="btn btn-success">Sign in</button>-->
<%for( i = 1 ; i <10 ;i++){%>
    <input type="hidden" id="tree<%=i%>" value="<%=Array[i]%>"/>
<%}%>

<%for( i = 1 ; i <10 ;i++){%>
<input type="hidden" id="tree_price<%=i%>" value="<%=price[i]%>"/>
<%}%>

<input type="hidden" id="user_gold" value="<%=user.getGold()%>"/>

<script>
    var user_id  = document.getElementById("id").value;
    var user_gold = parseInt(document.getElementById("user_gold").value);
    var buy = document.getElementById("buy");
    var btn_01 = document.getElementById("btn_01");
    var btn_02 = document.getElementById("btn_02");
    var btn_03 = document.getElementById("btn_03");
    var btn_04 = document.getElementById("btn_04");
    var flag = 2;
    var tree_id;
    var tree_state;
    var tree_price;
    //
    //
    // buy.onclick = function () {
    //     swal({
    //         title: "您确定购买这颗树吗",
    //         text: "一经购买，将无法退换",
    //         icon: "问号.png",
    //         buttons: true,
    //         dangerMode: "假的",
    //     })
    //         .then((willDelete) => {
    //             if (willDelete) {
    //                 swal("购买成功", {
    //                     icon: "success",
    //
    //                 });
    //             } else {
    //                 swal("取消购买");
    //             }
    //         });
    //     if(flag == 0)
    //      buytree();
    // }
    // function doit() {
    //     alert(id);
    // }
    // btn_04.onclick = function () {
    //     tree_id = document.getElementById("tree_id").value;
    //     alert("用户金币："+user_gold+"树的金额："+document.getElementById("tree_price"+tree_id).value);
    // }
    // function select_data(){
    //     $.ajax({
    //         url:"select_tree",
    //         type:"GET",
    //         data:{"id":id},
    //         success:function (data) {
    //         },error:function () {
    //             alert("失败了");
    //         }
    //     })
    // }

    function buytree( tree_price) {
        $.ajax({
            url:"buytree_sevlet",
            type:"GET",
            data:{"tree_id":tree_id,"user_id":user_id,"tree_price":tree_price},
            success:function (data) {
                alert("购买成功")
            },error:function () {
                alert("购买失败");
            }
        })
    }

    function selltree(treeprice) {
        $.ajax({
            url:"selltree_servlet",
            type:"GET",
            data:{"tree_id":tree_id,"user_id":user_id,"treeprice":treeprice},
            success:function (data) {
                alert("出售成功")
            },error:function () {
                alert("出售失败");
            }
        })
    }
    function usetree(){
        $.ajax({
            url:"usetree_servlet",
            type:"GET",
            data:{"tree_id":tree_id,"user_id":user_id},
            success:function (data) {
                alert("使用该树成功");
            },error:function () {
                alert("使用失败");
            }
        })
    }

    btn_01.onclick = function myFunction(){
        tree_id = document.getElementById("tree_id").value;
        tree_price =parseInt( document.getElementById("tree_price"+tree_id).value);
        tree_state = document.getElementById("tree"+tree_id).value;
        var r = confirm("确认购买？");
        if (r==true && tree_state == 0 && tree_price <= user_gold){
            buytree(tree_price);
        }
        else if (tree_state == 1){
            alert("购买失败，您已购买此树");
        }
        else if(tree_price >= user_gold){
            alert("购买失败，您的金币不足");
        }else {
            alert("未知，购买失败");
        }
    }

    btn_02.onclick = function sellFunction(){
        tree_id = document.getElementById("tree_id").value;
        tree_state = document.getElementById("tree"+tree_id).value;
        tree_price =parseInt( document.getElementById("tree_price"+tree_id).value);
        var x;
        var r = confirm("确认卖出？");
        if (r==true && tree_state == 1){

            selltree(tree_price);
        }
        else{
            alert("售卖失败,您还未购买此树");
        }
    }

    btn_03.onclick = function useFunction(){
        tree_id = document.getElementById("tree_id").value;
        tree_state = document.getElementById("tree"+tree_id).value;
        var x;
        var r = confirm("确认使用？");
        if (r==true && tree_state == 1){
            usetree();
        }
        else{
            alert("使用失败，您还未购买此树");
        }
    }
</script>
</body>
</html>